package com.test.daily.leetcode.y2022.m04.day0421.v02;

/**
 * @author Tom on 2022/4/21
 */
public class Solution {
    static class Node{
        int val;
        Node next;
        public Node(){
        }
    }
    public static void main(String[] args) {
        Node n = new Node();
        n.val = 1;
        n.next = new Node();
        n.next.val = 2;
        n.next.next = new Node();
        n.next.next.val = 3;
        Solution s = new Solution();
        Node reversed = s.reverse(n);
        while(reversed != null){
            System.out.print("  " + reversed.val);
            reversed = reversed.next;
        }
    }
    // 单链表逆序
    public Node reverse(Node head){
        Node pre = null;
        Node next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}
